<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <base href="<%=request.getContextPath()%>/">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>支付管理 - 医疗预约系统后台</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="stylesheet" href="statics/css/hisStyle.css">
    <link rel="stylesheet" href="statics/css/common-styles.css">
</head>
<body>
<!-- 侧边栏 -->
<%@include file="../common/sidebar.jsp"%>

<!-- 主内容区域 -->
<div class="main-content">
    <!-- 顶部导航 -->
<%@include file="../common/top.jsp"%>

    <!-- 内容区域 -->
    <div class="content">
        <div class="function-page">
            <!-- 页面标题 -->
            <div class="page-header">
                <h1>支付管理</h1>
            </div>

            <!-- 查询表单 -->
            <div class="search-filter">
                <form id="payFindForm" action="pay/find" method="get">
                    <div class="form-row">
                        <div class="form-group">
                            <label for="patientName">患者姓名：</label>
                            <input type="text" id="patientName" name="patientName" value="${param.patientName}" placeholder="请输入患者姓名" class="form-control"/>
                        </div>
                        <div class="form-group">
                            <label for="status">支付状态：</label>
                            <select id="status" name="status" class="form-control">
                                <option value="">全部</option>
                                <option value="0" <c:if test="${param.status eq '0'}">selected</c:if>>待支付</option>
                                <option value="1" <c:if test="${param.status eq '1'}">selected</c:if>>已支付</option>
                                <option value="2" <c:if test="${param.status eq '2'}">selected</c:if>>已退款</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="payDate">支付日期：</label>
                            <input type="date" id="payDate" name="payDate" value="${param.payDate}" class="form-control"/>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-search"></i> 查询
                            </button>
                            <span class="search-info">总计 ${pageInfo.total} 条支付记录</span>
                        </div>
                    </div>
                </form>
            </div>

            <!-- 支付记录列表 -->
            <div class="data-section">
                <table class="data-table">
                <thead>
                <tr>
                    <th>订单号</th>
                    <th>患者姓名</th>
                    <th>科室</th>
                    <th>金额(元)</th>
                    <th>支付方式</th>
                    <th>支付状态</th>
                    <th>支付时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${pageInfo.list}" var="pay">
                    <tr>
                        <td>${pay.orderNo}</td>
                        <td>${pay.appointment.patientName}</td>
                        <td>${pay.appointment.visit.department.deptName}</td>
                        <td>${pay.amount}</td>
                        <td>${pay.payMethod}</td>
                        <td>
                            <c:choose>
                                <c:when test="${pay.status eq 0}"><span class="status status-warning">待支付</span></c:when>
                                <c:when test="${pay.status eq 1}"><span class="status status-normal">已支付</span></c:when>
                                <c:when test="${pay.status eq 2}"><span class="status status-danger">已退款</span></c:when>
                                <c:otherwise><span class="status">未知</span></c:otherwise>
                            </c:choose>
                        </td>
                        <td>${pay.payTime}</td>
                        <td>
                            <a href="javascript:void(0)" class="btn btn-secondary btn-sm" onclick="findById(${pay.payId})">
                                <i class="fas fa-search"></i> 查看
                            </a>
                            <!-- 只有病人用户(type=1)且支付状态为待支付时才显示支付按钮 -->
                            <c:if test="${pay.paystatus eq 0 && loginUser.type eq 1}">
                                <a href="javascript:void(0)" class="btn btn-primary btn-sm pay-btn" onclick="payNow(${pay.payId}, '${pay.appointment.number}')">
                                    <i class="fas fa-credit-card"></i> 支付
                                </a>
                            </c:if>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
            </div>
            
            <!-- 分页控件 -->
            <span class="pagination-info">共 ${pageInfo.total} 条记录，当前第 ${pageInfo.pageNum} / ${pageInfo.pages} 页</span>
            <div class="pagination-container">
                <div class="pagination">
                    <a href="pay/find?pageNum=1&patientName=${param.patientName}&status=${param.status}&payDate=${param.payDate}" 
                       class="pagination-link">
                       <i class="fas fa-angle-double-left"></i> 首页
                    </a>
                    <a href="pay/find?pageNum=${pageInfo.pageNum-1}&patientName=${param.patientName}&status=${param.status}&payDate=${param.payDate}" 
                       class="pagination-link ${pageInfo.pageNum == 1 ? 'disabled' : ''}">
                       <i class="fas fa-angle-left"></i> 上一页
                    </a>
                    
                    <!-- 页码遍历 -->
                    <c:forEach begin="1" end="${pageInfo.pages}" var="num">
                        <a href="pay/find?pageNum=${num}&patientName=${param.patientName}&status=${param.status}&payDate=${param.payDate}" 
                           class="pagination-link ${pageInfo.pageNum == num ? 'active' : ''}">
                           ${num}
                        </a>
                    </c:forEach>
                    
                    <c:if test="${pageInfo.pageNum < pageInfo.pages}">
                        <a href="pay/find?pageNum=${pageInfo.pageNum+1}&patientName=${param.patientName}&status=${param.status}&payDate=${param.payDate}" 
                           class="pagination-link">
                           <i class="fas fa-angle-right"></i> 下一页
                        </a>
                        <a href="pay/find?pageNum=${pageInfo.pages}&patientName=${param.patientName}&status=${param.status}&payDate=${param.payDate}" 
                           class="pagination-link">
                           <i class="fas fa-angle-double-right"></i> 末页
                        </a>
                    </c:if>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    // 查看支付详情
    function findById(payId) {
        window.location.href = "pay/findById?payId=" + payId;
    }

    // 立即支付
    function payNow(payId, appointmentNo) {
        window.location.href = "pay/confirmPay?payId=" + payId;
    }
</script>
</body>
</html>